package com.linglan.bff.controller.app;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.linglan.bff.vo.app.doctor.DoctorListVO;
import com.linglan.bff.vo.app.feeback.FeedBackListVO;
import com.linglan.common.api.Page;
import com.linglan.common.api.Query;
import com.linglan.common.api.R;
import com.linglan.common.utils.PageUtil;
import com.linglan.modules.doctor.dto.result.DoctorResult;
import com.linglan.modules.doctor.service.BizDoctorService;
import com.linglan.modules.feedback.dto.result.FeedbackResult;
import com.linglan.modules.feedback.service.BizFeedbackService;
import com.linglan.modules.user.dto.result.PatientResult;
import com.linglan.modules.user.service.PatientService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 评价相关业务模块
 */
@RestController
@RequestMapping("/app/feedbacks")
@Slf4j
public class FeedbackController {
    @Resource
    private BizFeedbackService feedbackService;
    @Resource
    private BizDoctorService doctorService;
    @Resource
    private PatientService patientService;

    /**
     * 分页查询全部评价列表
     *
     * @param query
     * @return
     */
    @GetMapping("/page")
    @SaIgnore
    public R<Page<FeedBackListVO>> findByPage(Query query) {
        Page<FeedbackResult> resultPage = feedbackService.findByPage(query, null);
        Page<FeedBackListVO> voPage = PageUtil.copyPurePage(resultPage);
        List<FeedbackResult> records = resultPage.getRecords();
        List<FeedBackListVO> voList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(records)) {
            List<Long> userIds = records.stream().map(FeedbackResult::getUserId).collect(Collectors.toList());
            Map<Long, PatientResult> patientResultMap = patientService.findGroupById(userIds);
            List<Long> doctorIds = records.stream().map(FeedbackResult::getDoctorId).collect(Collectors.toList());
            Map<Long, DoctorResult> doctorResultMap = doctorService.findGroupById(doctorIds);
            for (FeedbackResult record : records) {
                FeedBackListVO vo = new FeedBackListVO();
                vo.setId(record.getId().toString());
                Long userId = record.getUserId();
                PatientResult patientResult = patientResultMap.get(userId);
                if (patientResult != null) {
                    vo.setPatientName(patientResult.getName());
                }
                Long doctorId = record.getDoctorId();
                DoctorResult doctorResult = doctorResultMap.get(doctorId);
                if (doctorResult != null) {
                    vo.setDoctorName(doctorResult.getName());
                }
                if (record.getScore() != null) {
                    vo.setScore(record.getScore().toString());
                }
                vo.setContent(record.getContent());
                vo.setCreatedAt(DateUtil.format(record.getCreatedAt(), DatePattern.NORM_DATETIME_MINUTE_PATTERN));
                voList.add(vo);
            }
        }
        voPage.setRecords(voList);
        return R.ok(voPage);
    }

    /**
     * 根据科室分页查询评价列表
     *
     * @param query
     * @return
     */
    @GetMapping("/{departmentId}/page")
    @SaIgnore
    public R<Page<FeedBackListVO>> findByDepartmentIdPage(@PathVariable Long departmentId, Query query) {
        Page<FeedbackResult> resultPage = feedbackService.findByPage(query, departmentId);
        Page<FeedBackListVO> voPage = PageUtil.copyPurePage(resultPage);
        List<FeedbackResult> records = resultPage.getRecords();
        List<FeedBackListVO> voList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(records)) {
            List<Long> userIds = records.stream().map(FeedbackResult::getUserId).collect(Collectors.toList());
            Map<Long, PatientResult> patientResultMap = patientService.findGroupById(userIds);
            List<Long> doctorIds = records.stream().map(FeedbackResult::getDoctorId).collect(Collectors.toList());
            Map<Long, DoctorResult> doctorResultMap = doctorService.findGroupById(doctorIds);
            for (FeedbackResult record : records) {
                FeedBackListVO vo = new FeedBackListVO();
                vo.setId(record.getId().toString());
                Long userId = record.getUserId();
                PatientResult patientResult = patientResultMap.get(userId);
                if (patientResult != null) {
                    vo.setPatientName(patientResult.getName());
                }
                Long doctorId = record.getDoctorId();
                DoctorResult doctorResult = doctorResultMap.get(doctorId);
                if (doctorResult != null) {
                    vo.setDoctorName(doctorResult.getName());
                }
                if (record.getScore() != null) {
                    vo.setScore(record.getScore().toString());
                }
                vo.setContent(record.getContent());
                vo.setCreatedAt(DateUtil.format(record.getCreatedAt(), DatePattern.NORM_DATETIME_MINUTE_PATTERN));
                voList.add(vo);
            }
        }
        voPage.setRecords(voList);
        return R.ok(voPage);
    }
}
